package com.zhengke.controller.archie;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.zhengke.pojo.archie.CurrentUser;
import com.zhengke.pojo.archie.GoodsDetail;
import com.zhengke.pojo.archie.OrderAndGoods;
import com.zhengke.pojo.archie.OrderDetail;
import com.zhengke.pojo.archie.OrderPagination;
import com.zhengke.service.archie.IOrderDetailService;

/**
 * @author 黄保富
 * @date 2020年1月14日  
 * @version 1.0.0 
 * 
 */
@Controller
public class OrderController extends OrderPagination {
	
	//注入对象
	@Autowired
	IOrderDetailService orderService;
	
	@RequestMapping("ordersData.do")//请求订单数据的方法
	public void orderDataMethod(HttpServletRequest request,HttpServletResponse response) {
		//获取用户的tvnNo号
		CurrentUser user =	getCurrentUser(request, response);
		String user_id = user.getTvnNo();
		try {
			//调用service层的方法进行获取订单集合
			List<OrderDetail> orderDetailList = orderService.orderListDetailList(user_id);
			List<Map<String,String>> rtnList = new ArrayList<Map<String,String>>();
			for(OrderDetail or : orderDetailList) {
				Map<String,String> temp = new HashMap<String, String>();
				//通过id查询订单对应的商品信息
				GoodsDetail  goodsDetail =	orderService.querryGoodsByOrderId(or.getOrder_id());
				
				if(goodsDetail != null) {
					if(goodsDetail.getGoods_type() == "3" || goodsDetail.getGoods_type() == "7") {
						temp.put("goodsAmount",or.getGoods_amount()+"分");
						temp.put("orderAmount",or.getOrder_amount()+"分");
					}else {
						temp.put("goodsAmount","￥"+or.getGoods_amount()+"分");
						temp.put("orderAmount","￥"+or.getOrder_amount()+"分");
					}
					temp.put("goodsId",goodsDetail.getGoods_id());
					temp.put("orderNumber", goodsDetail.getGoods_num());
				}	
				temp.put("orderSn",or.getOrder_sn());
				temp.put("orderId",or.getOrder_id());
				temp.put("addTime",or.getAdd_time());
				temp.put("finishTime",or.getFinish_time());
				switch (or.getOrder_state()) {
				case "0":
					temp.put("orderState","已取消");
					break;
				case "10":
					temp.put("orderState","待确认");
					break;
				case "20":
					temp.put("orderState","已确认");
					break;
				case "30":
					temp.put("orderState","已配货");
					break;
				case "40":
					temp.put("orderState","已发货");
					break;
				case "50":
					temp.put("orderState","已收款");
					break;
				case "60":
					temp.put("orderState","已退货");
					break;
				}
				rtnList.add(temp);
			}
			response.setCharacterEncoding("utf-8");
			response.setHeader("Pragma","No-cache"); 
			response.setHeader("Cache-Control","no-cache"); 
			response.setDateHeader("Expires", 0);  
			response.getWriter().print("var dataList="+JSONArray.toJSON(rtnList));	
		}catch (Exception e) {
			e.getStackTrace();
		}
	}

	/**
	    *    跳转到订单详情的页面,跳转的过程中就携带值到对应的界面
	 */
	@RequestMapping("toOrderDetail.do")
	public String toOrderDetailMethod(HttpServletRequest request,HttpServletResponse response,String orderId,ModelMap model) {
		HttpSession session = request.getSession();
		//根据订单的id进行查询订单的详细信息的方法
		List<Map<String, Object>> list =  orderService.orderDetailQuerryById(orderId);
		Map<String, Object> orderDetailMap =  list.get(0);
		//对商品信息及进行更改  orderDetailMap
		String state = orderDetailMap.get("order_state").toString();//状态
		String goodsTye = orderDetailMap.get("goods_type").toString();//类型
		System.out.println("///////////////////////////////////////////////////////////////////////"+state);
		System.out.println("```````````````````````````````````````````````````````````````````````"+goodsTye);
		System.out.println("-------0000000099999----------"+orderDetailMap.isEmpty());
			switch (state) {
			case "0":
				//session.setAttribute("state", "已取消");
				model.put("state", "已取消");
				break;
			case "10":
				//session.setAttribute("state", "待确认");
				model.put("state", "待确认");
				break;
			case "20":
				//session.setAttribute("state", "已确认");
				model.put("state", "已确认");
				break;
			case "30":
				//session.setAttribute("state", "已配货");
				model.put("state", "已配货");
				break;
			case "40":
				//session.setAttribute("state", "已发货");
				model.put("state", "已发货");
				break;
			case "50":
				//session.setAttribute("state", "已收款");
				model.put("state", "已收款");
				break;
			case "60":
				//session.setAttribute("state", "已退货");
				model.put("state", "已退货");
				break;
			}
			
			switch (goodsTye) {
			case "0" :
				model.put("goodsType","正品");
				//session.setAttribute("goodsType","正品");
				break;
			case "1" :
				model.put("goodsType","打折品");
				//session.setAttribute("goodsType","打折品");
				break;
			case "2" :
				model.put("goodsType","体验品");
				//session.setAttribute("goodsType","体验品");
				break;
			case "3" :
				model.put("goodsType","积分兑换商品");
				//session.setAttribute("goodsType","积分兑换商品");
				break;
			case "4" :
				model.put("goodsType","已发货");
				//session.setAttribute("goodsType","已发货");
				break;
			case "5" :
				model.put("goodsType","打包品");
				//session.setAttribute("goodsType","打包品");
				break;
			case "7" :
				model.put("goodsType","积分兑换商品");
				//session.setAttribute("goodsType","积分兑换商品");
				break;
			}
			OrderAndGoods orderAndGoods = new OrderAndGoods();
			orderAndGoods.setOrder_sn(orderDetailMap.get("order_sn").toString());
			orderAndGoods.setOrder_state(orderDetailMap.get("order_state").toString());
			orderAndGoods.setOrder_amount(orderDetailMap.get("order_amount").toString());
			orderAndGoods.setReceiver_address(orderDetailMap.get("receiver_address").toString());
			orderAndGoods.setReceipt_method(orderDetailMap.get("receipt_method").toString());
			orderAndGoods.setGoods_amount(orderDetailMap.get("goods_amount").toString());
			orderAndGoods.setGoods_name(orderDetailMap.get("goods_name").toString());
			orderAndGoods.setService_point_id(orderDetailMap.get("service_point_id").toString());
			orderAndGoods.setGoods_num(orderDetailMap.get("goods_num").toString());
			orderAndGoods.setGoods_id(orderDetailMap.get("goods_id").toString());
			model.put("list",orderAndGoods);
			model.put("goodsId",orderDetailMap.get("goods_id").toString());
			model.put("num",orderDetailMap.get("order_amount").toString());
			model.put("type",orderDetailMap.get("goods_type").toString());
		return "tv/accountCenter/orderDetail";
	}
}
